{% extends "analysis_plugins/general_information.html" %}


{% block styles %}
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/code_pre.css') }}">
    <style>
        .qemu-td {
            width: 100%;
            max-width: 0;
        }
    </style>
{% endblock %}


{% block analysis_result_details %}

    {% if 'files' in analysis_result %}

        <th class="success" colspan="2">
            Results for Included Files
        </th>

        {% for uid, result in analysis_result['files'].items() %}

            <tr data-toggle="collapse" data-target="#collapse-{{ uid }}">
                <td class="clickable" colspan=2>
                    <i class="fas fa-angle-down"></i> {{ result['path'] }}
                    {% if result['executable'] %}
                        <i class="fas fa-check text-success"></i>
                    {% else %}
                        <i class="fas fa-times text-danger"></i>
                    {% endif %}
                </td>
            </tr>

            <tr>
                <td colspan="2" class="qemu-td p-0">
                    <div class="collapse" id="collapse-{{ uid }}">

                        <table class="table table-bordered m-0" style="width: 100%">
                            <tr>
                                <td width="175px">Executable in QEMU:</td>
                                <td>{{ result['executable'] }}</td>
                            </tr>

                            {% if result['results'] %}
                            <tr>
                                <td>Individual Results:</td>
                                <td class="p-0">
                                    <table class="table table-bordered m-0" style="width: 100%">
                                    {% for arch in result['results'] %}
                                        <tr>
                                            {% if "strace" in result['results'][arch] %}
                                                <td style="width: 1px" rowspan="{{ (result['results'][arch] | length - 1) * 3  + 1 }}">
                                            {% else %}
                                                <td style="width: 1px" rowspan="{{ (result['results'][arch] | length) * 3 }}">
                                            {% endif %}
                                                {{ arch }}
                                            </td>
                                            {% for option in result['results'][arch] %}
                                                {% if option != 'strace' %}
                                                    {% set option_result = result['results'][arch][option] %}
                                                    <td style="width: 1px" rowspan="3">{{ option }}</td>
                                                    <td>stdout</td>
                                                    <td class="qemu-td" style="padding: 5px;">
                                                        {% if option_result['stdout'] %}
                                                        <pre class="code-pre">{{ '$ .' + result['path'] + ' ' + option + '\n' + option_result['stdout'] }}</pre>
                                                        {% else %}Empty{% endif %}
                                                    </td>
                                                    </tr><tr>
                                                        <td>stderr</td>
                                                        <td>
                                                            {% if option_result['stderr'] %}
                                                            <pre class="code-pre">{{ '$ .' + result['path'] + ' ' + option + '\n' + option_result['stderr'] }}</pre>
                                                            {% else %}Empty{% endif %}
                                                        </td>
                                                    </tr><tr>
                                                        <td>return code</td>
                                                        <td>{{ option_result['return_code'] }}</td>
                                                    </tr><tr>
                                                {% endif %}
                                            {% endfor %}
                                            {% if 'strace' in result['results'][arch] %}
                                                    <td style="width: 1px">strace</td>
                                                    <td colspan="2">
                                                        {% if result['results'][arch]['strace'] %}
                                                            <pre class="code-pre">{{ result['results'][arch]['strace'] | decompress }}</pre>
                                                        {% else %}Empty{% endif %}
                                                    </td>
                                                </tr>
                                            {% else %}
                                                </tr>
                                            {% endif %}
                                    {% endfor %}
                                    </table>
                                </td>
                            </tr>
                            {% endif %}
                        </table>

                    </div>
                </td>
            </tr>

        {% endfor %}
    {% endif %}

{% endblock %}


{% block ajax %}
    {% set analysis = analysis_result %}

    {% if 'parent_flag' in analysis and analysis.parent_flag == True %}
        <div id="file_results"></div>
        <script>
            $(document).ready(function() {
                $('#file_results').load('/plugins/qemu_exec/ajax/{{ firmware.uid }}');
            });
        </script>
    {% endif %}

{% endblock %}
